﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Model.Common;
using Chinaer.Core.Helper;

namespace Chinaer.Bll
{
    public class ZhongXinGongGao : Repository, Chinaer.IBll.IZhongXinGongGao
    {
        Chinaer.IDao.IZhongXinGongGaoDAO zhongXinGongGaoDAO = new Chinaer.Dao.ZhongXinGongGaoDAO();
        public List<DTO.ZhongXinGongGaoDTO> GetAllZhongXinGongGao(string shangJiaID)
        {
            List<Model.Entities.Model_ZhongXinGongGao> model_ListZhongXinGongGao = new List<Model.Entities.Model_ZhongXinGongGao>();
            model_ListZhongXinGongGao = zhongXinGongGaoDAO.GetAllZhongXinGongGao();
            List<DTO.ZhongXinGongGaoDTO> listZhongXinGongGaoDTO = new List<DTO.ZhongXinGongGaoDTO>();
            foreach (var item in model_ListZhongXinGongGao)
            {
                DTO.ZhongXinGongGaoDTO zhongXinGongGaoDTO = new DTO.ZhongXinGongGaoDTO();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, zhongXinGongGaoDTO);
                ///获取该商家对应该中心公告是否阅读
                Chinaer.IDao.IShangJiaIsReadZhongXinGongGaoDAO shangJiaIsReadZhongXinGongGao = new Dao.ShangJiaIsReadZhongXingGongGaoDAO();
                bool isRead = shangJiaIsReadZhongXinGongGao.GetShangJiaIsReadStatue(item.ID, shangJiaID);
                zhongXinGongGaoDTO.IsReadZhongXinGongGao = isRead;

                listZhongXinGongGaoDTO.Add(zhongXinGongGaoDTO);
            }
            return listZhongXinGongGaoDTO;
        }

        public List<DTO.ZhongXinGongGaoDTO> GetZhongXinGongGaoByTime(string shangJiaID, DateTime beginTime, DateTime endTime)
        {
            List<Model.Entities.Model_ZhongXinGongGao> model_ListZhongXinGongGao = new List<Model.Entities.Model_ZhongXinGongGao>();
            model_ListZhongXinGongGao = zhongXinGongGaoDAO.GetZhongXinGongGaoByTimeSpan(beginTime, endTime);
            List<DTO.ZhongXinGongGaoDTO> listZhongXinGongGaoDTO = new List<DTO.ZhongXinGongGaoDTO>();
            foreach (var item in model_ListZhongXinGongGao)
            {
                DTO.ZhongXinGongGaoDTO zhongXinGongGaoDTO = new DTO.ZhongXinGongGaoDTO();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, zhongXinGongGaoDTO);
                ///获取该商家对应该中心公告是否阅读
                Chinaer.IDao.IShangJiaIsReadZhongXinGongGaoDAO shangJiaIsReadZhongXinGongGao = new Dao.ShangJiaIsReadZhongXingGongGaoDAO();
                bool isRead = shangJiaIsReadZhongXinGongGao.GetShangJiaIsReadStatue(item.ID, shangJiaID);
                zhongXinGongGaoDTO.IsReadZhongXinGongGao = isRead;
                listZhongXinGongGaoDTO.Add(zhongXinGongGaoDTO);
            }
            return listZhongXinGongGaoDTO;
        }

        public JsonResult Add(DTO.ZhongXinGongGaoDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_ZhongXinGongGao model_ZhongXinGongGao = new Model.Entities.Model_ZhongXinGongGao();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_ZhongXinGongGao);
            if (string.IsNullOrEmpty(model_ZhongXinGongGao.ID) || model_ZhongXinGongGao.ID.Trim() == "")
            {
                model_ZhongXinGongGao.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            }
            jsonResult.Success = zhongXinGongGaoDAO.Add(model_ZhongXinGongGao);
            return jsonResult;
        }

        public JsonResult Delete(DTO.ZhongXinGongGaoDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_ZhongXinGongGao model_ZhongXinGongGao = new Model.Entities.Model_ZhongXinGongGao();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_ZhongXinGongGao);
            jsonResult.Success = zhongXinGongGaoDAO.Delete(model_ZhongXinGongGao);
            return jsonResult;
        }

        public JsonResult Delete(string id)
        {
            JsonResult jsonResult = new JsonResult();
            jsonResult.Success = zhongXinGongGaoDAO.Delete(id);
            return jsonResult;
        }

        public JsonResult Update(DTO.ZhongXinGongGaoDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_ZhongXinGongGao model_ZhongXinGongGao = new Model.Entities.Model_ZhongXinGongGao();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_ZhongXinGongGao);
            bool flag = false;
            bool isRead = t.IsReadZhongXinGongGao == null ? false : true;//确定是否已经阅读
            if (isRead == true)
            {
                //如果该商户已经阅读 则添加一条记录到已经阅读表中  表示已经阅读
                Model.Entities.Model_ShangJiaIsReadZhongXinTongGao model_ZhongXinGongGaoIsRead = new Model.Entities.Model_ShangJiaIsReadZhongXinTongGao();
                model_ZhongXinGongGaoIsRead.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
                model_ZhongXinGongGaoIsRead.CreateTime = DateTime.Now;
                model_ZhongXinGongGaoIsRead.IsDelete = false;
                model_ZhongXinGongGaoIsRead.IsRead = true;
                model_ZhongXinGongGaoIsRead.IsUse = true;
                model_ZhongXinGongGaoIsRead.ModifyTime = DateTime.Now;
                model_ZhongXinGongGaoIsRead.ReadTime = DateTime.Now;
                model_ZhongXinGongGaoIsRead.ShangJiaID = t.ShangJiaID;
                model_ZhongXinGongGaoIsRead.ZhongXinGongGaoID = t.ID;
                Chinaer.IDao.IShangJiaIsReadZhongXinGongGaoDAO shangJiaIsReadZhongXinGongGao = new Chinaer.Dao.ShangJiaIsReadZhongXingGongGaoDAO();
                flag = shangJiaIsReadZhongXinGongGao.Add(model_ZhongXinGongGaoIsRead);
            }

            flag = flag & zhongXinGongGaoDAO.Update(model_ZhongXinGongGao);
            jsonResult.Success = flag;
            return jsonResult;
        }

        public DTO.ZhongXinGongGaoDTO GetByID(string id)
        {
            Model.Entities.Model_ZhongXinGongGao model_ZhongXinGongGao = new Model.Entities.Model_ZhongXinGongGao();
            model_ZhongXinGongGao = zhongXinGongGaoDAO.GetByID(id);
            DTO.ZhongXinGongGaoDTO zhongXinGongGaoDTO = new DTO.ZhongXinGongGaoDTO();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_ZhongXinGongGao, zhongXinGongGaoDTO);
            return zhongXinGongGaoDTO;
        }
    }
}
